package com.example.jsd2205.vx.emos.task;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.Serializable;

/**
 * 发送邮件线程任务类
 * Scope("prototype")  将对象设置成多例对象,单例会发生线程安全的问题
 */
@Component
@Scope("prototype")
public class EmailTask implements Serializable {
    /**
     * javaMailSender 发送邮件
     */
    @Autowired
    private JavaMailSender javaMailSender;
    /**
     * 发件人邮箱
     */
    @Value("${emos.email.system}")
    private String mailbox;

    /**
     * 发送邮件的封装信息
     * Async 异步执行
     * @param message
     */
    @Async
    public void sendAsync(SimpleMailMessage message){
        message.setFrom(mailbox);
        message.setCc(mailbox);
        javaMailSender.send(message);
        System.out.println("发送邮件");
    }
}
